package com.trafficsys.modules.loginAndRegister.controller;

import com.trafficsys.modules.entity.IdCard;
import com.trafficsys.modules.loginAndRegister.config.ResourceConfigBean;
import com.trafficsys.modules.loginAndRegister.service.CommonService;
import com.trafficsys.modules.loginAndRegister.util.FindAllFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Zhao Bin
 * @Date 2021/9/28 13:26
 * @Version 1.0
 */
@Controller
@RequestMapping("/api")
public class CommonController {
    @Autowired
    private CommonService commonService;
    @Autowired//文件上传配置类
    private ResourceConfigBean resourceConfigBean;
    /*********************************************   以下公用功能  *********************************************************
     *  添加身份证信息    公用功能
     *  post:
     *  http://localhost:8080/api/addIdCard?cardId=500235199805059270&name=赵彬&birthday=1998-05-05&address=中国重庆云阳&department=重庆市公安局&state=在用
     {
     "cardId":"500235199805059271",
     "name":"赵彬",
     "birthday":"1998-05-05",
     "address":"中国重庆云阳",
     "department":"重庆市公安局",
     "state":"在用"
     }
     */
    @PostMapping(value = "/addIdCard", consumes = "application/json")
    @ResponseBody//将数据返回到body展示
    public HashMap<String,Object> addIdCard(@RequestBody IdCard idCard){return commonService.addIdCard(idCard); }
    /**
     * 发送手机验证码   公用功能
     *  post:
     *  http://localhost:8080/api/sendMessage?phone=15730593398
     */
    @PostMapping(value = "/sendMessage", consumes = "application/json")
    @ResponseBody//将数据返回到body展示
    public HashMap<String,Object> sendMessage(@RequestBody String phone){
        return commonService.sendMessage(phone);
    }
    /**
     * 上传文件   公用功能
     */
    @PostMapping(value = "/uploadFile", consumes = "multipart/form-data")
    @ResponseBody
    public HashMap<String,Object> uploadFile(@RequestBody MultipartFile file, String cardId) {
        HashMap<String,Object> map=new HashMap<>();
        if(file == null){
            map.put("info","您还没有选择文件！");
            return map;
        }
        //检查是否有对应用户的文件夹 没有则创建一个
        String path=String.format("%s%s", resourceConfigBean.getLocalPathForWindow(), cardId);
        File uploadFile=new File(path);
        if(!uploadFile.exists()){
            //创建文件夹
            uploadFile.mkdir();
        }
        //获取本地文件目录中的所有文件
        FindAllFile findAllFile = new FindAllFile();
        List<String> hadFileNames = findAllFile.findAllFile(path);
        for (String one : hadFileNames) {
            if (one.equals(file.getOriginalFilename())) {
                map.put("info",file.getOriginalFilename() + "文件已存在\n");
                return map;
            }
        }
        try {
            String destFilePath = String.format("%s%s%s",
                    resourceConfigBean.getLocalPathForWindow(), cardId+"/",file.getOriginalFilename());
            File destFile = new File(destFilePath);
            file.transferTo(destFile);
            map.put("info",file.getOriginalFilename() + "文件上传成功");
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            map.put("info",e.getMessage());
            return map;
        }
    }
}
